package com.yun.algorithmproblem.leetcode;

public class Leetcode3305 {

    public static void main(String[] args) {
        Leetcode3305 obj = new Leetcode3305();
        System.out.println(obj.countOfSubstrings("iqeaouqi", 2));
    }

    public int countOfSubstrings(String word, int k) {
        int[] cnt = new int[6];
        int ans = 0;
        int p = 0, q = 0;
        char[] chars = word.toCharArray();
        while (q < word.length()) {
            cnt[indexLetter(chars[q])]++;
            if (cnt[5] <= k) {
                if (cnt[5] == k && check(cnt, k)) {
                    ans++;
                }
            } else {
                while (cnt[5] > k) {
                    cnt[indexLetter(chars[p])]--;
                    if (check(cnt, k)) {
                        ans++;
                    }
                    p++;
                }
            }
            q++;
        }
        return ans;
    }

    private int indexLetter(char c) {
        int index;
        switch (c) {
            case 'a' -> index = 0;
            case 'e' -> index = 1;
            case 'i' -> index = 2;
            case 'o' -> index = 3;
            case 'u' -> index = 4;
            default -> index = 5;
        }
        return index;
    }

    private boolean check(int[] cnt, int k) {
        if (cnt[5] != k) {
            return false;
        }
        for (int i = 0; i < 5; i++) {
            if (cnt[i] == 0) {
                return false;
            }
        }
        return true;
    }
}
